
import { _decorator, Component, Node, Vec2, Animation, UITransform, Size, Vec3 } from 'cc';
import { ConfigKey, secondGun_specialSkill_config } from '../../manager/AppControll';
import { DamageStruct } from '../FightControl';
import { SkillOrigin } from './SkillOrigin';
import { MonsterManager } from '../monster/MonsterManager';
import { MonsterOrigin } from '../monster/MonsterOrigin';
import { SoundManager } from '../../manager/SoundManger';
import { DataHelper } from '../../system/DataHelper';
import { Tools } from '../../tools/Tools';
const { ccclass, property } = _decorator;


@ccclass('SkillLaser')
export class SkillLaser extends SkillOrigin {
    private isStart = false;
    private mStartPoint: Vec2 = null;
    private mEndPoint: Vec2 = null;

    InitSkill(config: secondGun_specialSkill_config, dmg: DamageStruct): void {
        super.InitSkill(config, dmg);
        this.InitAnimation(this.PlayAnimaition.bind(this));
    }

    InitAnData(startPoint: Vec2, mEndPoint: Vec2) {
        this.mEndPoint = mEndPoint;
        this.mStartPoint = startPoint;
    }

    PlayAnimaition() {
        if (!this.mEndPoint || !this.mSikllConfig) {
            this.node.destroy();
            return;
        }
        this.isStart = true;
        this.node.getComponent(UITransform).contentSize = new Size(1000, 400);
        let direction = this.mStartPoint.clone().subtract(this.mEndPoint).normalize();
        let angle = new Vec2(1, 0).angle(new Vec2(direction.x, direction.y));
        this.node.eulerAngles = new Vec3(0, 0, -angle / Math.PI * 180);
        this.node.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
            this.isStart = false;
            this.node.destroy();
        });


        if (this.mSikllConfig.aniAudio != "") {
            SoundManager.Instance<SoundManager>().PlayEffect("gun/" + this.mSikllConfig.aniAudio, Number.parseFloat(DataHelper.GetConfigByKey(ConfigKey.gunVolume).value));
        }
        
        this.node.getComponent(Animation).play(this.mSikllConfig.skillAni);
        this.GetBeingHit();
    }

    GetBeingHit() {
        const origin = this.mStartPoint;
        const destination = this.mEndPoint;
        Tools.GetLtMonster().forEach((elemt) => {
            let point = new Vec2(elemt.position.x, elemt.position.y);
            const direction = destination.clone().subtract(origin);
            const vector = point.clone().subtract(origin);
            const projection = vector.clone().project(direction);
            const perpendicular = origin.clone().add(projection);
            const distance = point.clone().subtract(perpendicular).length();
            if (distance < this.mSikllConfig.skillNum[0] * 100 / 2) {
                let dmg: DamageStruct = {
                    isCriticalHit: true,
                    damage: this.mDmg.damage,
                    time: 0.4,
                }
                elemt.getComponent(MonsterOrigin).Attacked(dmg);
            }
        })
    }



    start() {
        // [3]
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
